package com.qhs.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qhs.framework.bean.DataTableView;
import com.qhs.framework.bean.PageView;
import com.qhs.sys.entity.SysRoleInfo;
import com.qhs.sys.service.RoleService;
import com.qhs.framework.utils.JsonUtil;
import com.qhs.framework.utils.Result;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by xinchao on 2017/1/12 0012.
 */
@Controller
@RequestMapping(value = {"role"})
public class RoleInfoController {

    private final Logger logger = Logger.getLogger(getClass());

    @Autowired
    private RoleService roleService;


    /**
     * 查询获取菜单列表
     * @return
     */
    @RequestMapping(value = {"info"})
    public String index() {

        return "sys/role/index";
    }

    /**
     * 查询列表
     * @param sysRoleInfo
     * @param pageView
     * @return
     */
    @RequestMapping(value = {"list"})
    @ResponseBody
    public String searchList(SysRoleInfo sysRoleInfo, PageView pageView) {

        PageHelper.offsetPage(pageView.getStart(),pageView.getLength());
       // PageHelper.startPage(page, pageSize);
        List<SysRoleInfo> sysRoleInfos = roleService.findRoleByWhere(sysRoleInfo);

        PageInfo<SysRoleInfo> pageInfo = new PageInfo<>(sysRoleInfos);
        DataTableView dataTableView = new DataTableView();
        dataTableView.setDraw(pageView.getDraw());
        dataTableView.setRecordsTotal(new Long(pageInfo.getTotal()).intValue());
        dataTableView.setRecordsFiltered(new Long(pageInfo.getTotal()).intValue());
        dataTableView.setData(sysRoleInfos);

        return JsonUtil.toJson(dataTableView);
    }

    /**
     * 保存角色信息
     * @param sysRoleInfo
     * @return
     */
    @RequestMapping(value = "/add", method= RequestMethod.POST)
    @ResponseBody
    public Result addMenu(SysRoleInfo sysRoleInfo) {

        String valMes= validateForm(sysRoleInfo);
        if(StringUtils.isNotEmpty(valMes)){
            return new Result(valMes);
        }

         if(roleService.insert(sysRoleInfo)==0){
            return new Result("保存失败");
        }

        return new Result(true,"保存成功");

    }


    /**
     * 修改菜单信息
     * @param sysRoleInfo
     * @return
     */
    @RequestMapping(value = "/edit", method= RequestMethod.POST)
    @ResponseBody
    public Result updateRole(SysRoleInfo sysRoleInfo) {

        String valMes= validateForm(sysRoleInfo);
        if(StringUtils.isNotEmpty(valMes)){
            return new Result(valMes);
        }

        if(roleService.updateByPrimaryKeySelective(sysRoleInfo)==0){
            return new Result("修改失败");
        }
        return new Result(true,"修改成功");

    }


    /**
     * 查看
     * @param id
     * @return
     */
    @RequestMapping(value="/{id}", method= RequestMethod.GET)
    @ResponseBody
    public SysRoleInfo viewPage(@PathVariable Long id) {
        SysRoleInfo sysRoleInfo = roleService.selectByPrimaryKey(id);
        return sysRoleInfo;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value="/{id}", method= RequestMethod.POST)
    @ResponseBody
    public Result delete(@PathVariable Long id) {

        if(roleService.deleteByPrimaryKey(id)==0){
            return new Result("删除失败");
        }
        return new Result(true,"删除成功");
    }


    @RequestMapping(value = "/auth", method= RequestMethod.POST)
    @ResponseBody
    public Result authRole(@RequestParam(value = "ids[]" )Long[] ids, Long userId) {

        if(roleService.authRole(ids, userId)==0){
            return new Result("授权失败");
        }
        return new Result(true,"授权成功");

    }

    @RequestMapping(value = "/authMenu", method= RequestMethod.POST)
    @ResponseBody
    public Result authMenu(@RequestParam(value = "ids[]" )Long[] ids, Long roleId){
        if(roleService.authMenu(ids,roleId)==0){
            return new Result(false,"授权失败");
        }else{
            return new Result(true,"授权成功");
        }
    }

    /**
     * 校验提交信息
     * @param sysRoleInfo
     * @return
     */
    private String validateForm(SysRoleInfo sysRoleInfo){
        String msg = "";
        if(StringUtils.isEmpty(sysRoleInfo.getRoleName())){
            msg= "角色名称不能为空";
        }
        if(StringUtils.isEmpty(sysRoleInfo.getRoleCode())){
            msg= "角色编码不能为空";
        }
        return msg;
    }






}
